﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace System
{
    public class DelegateCallerMonitor
    {
        public static void ActionCaller(Delegate action,
                [CallerMemberName] string memberName = default,
                [CallerFilePath] string sourceFilePath = default,
                [CallerLineNumber] int sourceLineNumber = default(int), params object[] args)
        {
            var caller = new CallerMonitor(memberName, sourceFilePath, sourceLineNumber).Start();
            if (args != null && args.Length > 0)
            {
                action.DynamicInvoke(args);
            }
            else
            {
                action.DynamicInvoke();
            }
            caller.Stop();
        }

        public static object FunctionCaller(Delegate function,
            [CallerMemberName] string memberName = default,
            [CallerFilePath] string sourceFilePath = default,
            [CallerLineNumber] int sourceLineNumber = default(int), params object[] args)
        {
            var caller = new CallerMonitor(memberName, sourceFilePath, sourceLineNumber).Start();

            object result = null;

            if (args != null && args.Length > 0)
            {
                result = function.DynamicInvoke(args);
            }
            else
            {
                result = function.DynamicInvoke();
            }
            caller.Stop();
            return result;
        }
    }
}
